package com.qiuzhiyi.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.utils.TransformUtil;
import com.li.widget.TopNavigationBar;
import com.li.widget.VerticalDoubleText;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.ImageAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Author: Li
 * Date: 2017/11/21 0021
 * Description: 岗位
 */

public class PostActivity extends QZYBaseActivity {


    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.txt_type)
    TextView mTxtType;
    @BindView(R.id.layout_content)
    LinearLayout mLayoutContent;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    private long mClickTime = 0;
    private LinkedHashMap<String, String> mStringMap;
    private ArrayList<String> mList;
    private ImageAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_post;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mStringMap = new LinkedHashMap<>();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mTxtType.setText(bundle.getString("jobType"));
            mList = bundle.getStringArrayList("jobPic");
            mStringMap.put("工作内容", bundle.getString("jobContent"));
            mStringMap.put("工作环境", bundle.getString("jobEnv"));
            mStringMap.put("工作时间", bundle.getString("jobTime"));
            mStringMap.put("提示", bundle.getString("jobRemind"));


            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
            params.setMargins(0, 0, 0, TransformUtil.dp2px(mContext, 10));
            for (String s : mStringMap.keySet()) {
                VerticalDoubleText doubleText = new VerticalDoubleText(mContext);
                doubleText.setTitle(s);
                doubleText.setContent(mStringMap.get(s));
                doubleText.setPadding(TransformUtil.dp2px(mContext, 10), 0, 0, 0);
                mLayoutContent.addView(doubleText, params);
            }

            mRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
            mRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    GridLayoutManager manager = ((GridLayoutManager) parent.getLayoutManager());
                    int spanCount = manager.getSpanCount();
                    outRect.bottom = TransformUtil.dp2px(mContext, 5);
                    if ((parent.getChildAdapterPosition(view) + 1) % spanCount == 0) {
                        outRect.right = TransformUtil.dp2px(mContext, 0);
                    } else {
                        outRect.right = TransformUtil.dp2px(mContext, 5);
                    }
                }
            });
            mAdapter = new ImageAdapter(R.layout.item_img);
            mAdapter.bindToRecyclerView(mRecycler);
            mAdapter.setNewData(mList);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - mClickTime < 500) {
                        mClickTime = 0;
                        Bundle bundle = new Bundle();
                        bundle.putStringArrayList("0", mList);
                        bundle.putInt("1", position);
                        startWithArg(ImageListActivity.class, bundle);
                    } else {
                        mClickTime = currentTime;
                    }
                }
            });
        }

    }
    @OnClick(R.id.txt_more)
    public void onViewClicked() {
        if (mList != null && mList.size()>0) {
            Bundle bundle=new Bundle();
            bundle.putStringArrayList("0",mList);
            bundle.putInt("1", 0);
            startWithArg(ImageListActivity.class,bundle);
        }else {
            showToast("暂无图片");
        }
    }
}
